package com.tk;

import ch.qos.logback.core.db.DBHelper;
import com.tk.bean.Pet;
import com.tk.bean.User;
import com.tk.config.MyConfig;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.SpringBootConfiguration;
import org.springframework.boot.autoconfigure.AutoConfigurationExcludeFilter;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.context.TypeExcludeFilter;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.FilterType;

@SpringBootApplication(scanBasePackages = "com")
//@SpringBootConfiguration
//@EnableAutoConfiguration
//@ComponentScan("com")
public class Boot01HelloworldApplication {

    public static void main(String[] args) {
        ConfigurableApplicationContext run = SpringApplication.run(Boot01HelloworldApplication.class, args);
        String[] names = run.getBeanDefinitionNames();

        for (String name : names) {
//            System.out.println(name);
        }

        //@Configuration(proxyBeanMethods = true) 保持组件单实例
        Pet tom01 = run.getBean("tom", Pet.class);
        Pet tom02 = run.getBean("tom", Pet.class);
        System.out.println("组件：" + (tom01 == tom02));


        MyConfig bean = run.getBean(MyConfig.class);
        System.out.println(bean);

        User user = bean.user01();
        User user1 = bean.user01();
        System.out.println(user == user1);

        User user01 = run.getBean("user01", User.class);
        Pet tom = run.getBean("tom", Pet.class);
        System.out.println("用户的宠物：" + (user01.getPet() == tom));

        //5、获取组件
        String[] beanNamesForType = run.getBeanNamesForType(User.class);
        System.out.println("-----------------------");
        for (String s : beanNamesForType) {
            System.out.println(s);
        }

        DBHelper dbHelper = run.getBean(DBHelper.class);
        System.out.println(dbHelper);

        boolean haha = run.containsBean("haha");
        boolean hehe = run.containsBean("hehe");
        System.out.println("haha: " + haha);
        System.out.println("hehe: " + hehe);
    }

}
